#include "OldMatch.h"
#include "People.h"
#include "Team.h"
#include "RandomSystem.h"

namespace
{
	const int G_SAVE = 0;

	const int G_SHOT_SUPER = -60;
	const int G_SAVE_SUPER_SUPER = -30;
	const int G_SAVE_SUPER_GOOD = -10;
	const int G_SAVE_SUPER_MISSED = 10;
	const int G_SAVE_SUPER_RIDIC = 60;

	const int G_SHOT_HARD = -30;
	const int G_SAVE_HARD_SUPER = -40;
	const int G_SAVE_HARD_GOOD = -20;
	const int G_SAVE_HARD_MISSED = 5;
	const int G_SAVE_HARD_RIDIC = 40;

	const int G_SHOT_NORM = -5;
	const int G_SAVE_NORM_SUPER = -50;
	const int G_SAVE_NORM_GOOD = -25;
	const int G_SAVE_NORM_FEW = -5;
	const int G_SAVE_NORM_MISSED = 5;
	const int G_SAVE_NORM_RIDIC = 30;

	const int G_SHOT_EASY = 0;
	const int G_SAVE_EASY_SUPER = -50;
	const int G_SAVE_EASY_FEW = -10;
	const int G_SAVE_EASY_MISSED = 5;
	const int G_SAVE_EASY_RIDIC = 20;

	const int G_SAVE_POST = 10;
	const int G_SAVE_OUT = 30;

	// attacks
}

void OldMatch::CalcShotEval(const int i_team, const int i_shootingPlayer, const int i_attack, const int i_save)
{
	const int atk = m_data[i_team].m_BaseAttackSkills[i_shootingPlayer] * GetConditionPercent(i_team, i_shootingPlayer);
	const int dTeam = 1-i_team;
	const int gk = m_data[dTeam].m_gkSkill;
	if(i_attack <= G_SHOT_SUPER)
	{
		UpdateEval(i_team, i_shootingPlayer, 2, true);
		if(i_save <= G_SAVE_SUPER_SUPER)		UpdateEval(dTeam, 0, 4, true);
		else if(i_save <= G_SAVE_SUPER_GOOD)	UpdateEval(dTeam, 0, 3, true);
		else if(i_save <= G_SAVE)				UpdateEval(dTeam, 0, 2, true);
		else if(i_save <= G_SAVE_SUPER_MISSED)	UpdateEval(dTeam, 0, 1, true);
		else if(i_save <= G_SAVE_SUPER_RIDIC)	{}
		else									UpdateEval(dTeam, 0, 1, false);
	}
	else if(i_attack <= G_SHOT_HARD)
	{
		UpdateEval(i_team, i_shootingPlayer, 1, true);
		if(i_save <= G_SAVE_HARD_SUPER)			UpdateEval(dTeam, 0, 3, true);
		else if(i_save <= G_SAVE_HARD_GOOD)		UpdateEval(dTeam, 0, 2, true);
		else if(i_save <= G_SAVE)				UpdateEval(dTeam, 0, 1 + RandSelect(50, gk), true);
		else if(i_save <= G_SAVE_HARD_MISSED)	UpdateEval(dTeam, 0, RandSelect(50, gk), true);
		else if(i_save <= G_SAVE_HARD_RIDIC)	{}
		else									UpdateEval(dTeam, 0, 1, false);
	}
	else if(i_attack <= G_SHOT_NORM)
	{
		if(i_save <= G_SAVE_NORM_SUPER)			UpdateEval(dTeam, 0, 3, true);
		else if(i_save <= G_SAVE_NORM_GOOD)		UpdateEval(dTeam, 0, 1 + RandSelect(50, gk), true);
		else if(i_save <= G_SAVE_NORM_FEW)		UpdateEval(dTeam, 0, 1, true);
		else if(i_save <= G_SAVE)				UpdateEval(dTeam, 0, RandSelect(50, gk), true);
		else if(i_save <= G_SAVE_NORM_MISSED)	{}
		else if(i_save <= G_SAVE_NORM_RIDIC)	UpdateEval(dTeam, 0, 1, false);
		else									UpdateEval(dTeam, 0, 2, false);
	}
	else if(i_attack < G_SHOT_EASY)
	{
		UpdateEval(i_team, i_shootingPlayer, RandSelect(atk, 50), false);

		if(i_save <= G_SAVE_EASY_SUPER)			UpdateEval(dTeam, 0, 2, true);
		else if(i_save <= G_SAVE_EASY_FEW)		UpdateEval(dTeam, 0, 1, true);
		else if(i_save <= G_SAVE)				UpdateEval(dTeam, 0, RandSelect(50, gk), true);
		else if(i_save <= G_SAVE_EASY_MISSED)	UpdateEval(dTeam, 0, 1, false);
		else if(i_save <= G_SAVE_EASY_RIDIC)	UpdateEval(dTeam, 0, 2, false);
		else									UpdateEval(dTeam, 0, 3, false);
	}
	else if(i_attack < G_SAVE_POST)
		UpdateEval(i_team, i_shootingPlayer, RandSelect(50, atk), true);
	else if(i_attack < G_SAVE_OUT)
		UpdateEval(i_team, i_shootingPlayer, RandSelect(atk, 50), false);
	else
		UpdateEval(i_team, i_shootingPlayer, 1 + RandSelect(atk, 50), false);
}

void OldMatch::CalcActionEval(const int i_atkTeam, const int i_defTeam, const int i_atkIndex, 
	const int i_defIndex, const int i_atkResult, const int i_defResult)
{
	const int result = i_atkResult - i_defResult;
	const int atk = m_data[i_atkTeam].m_BaseAttackSkills[i_atkIndex] * GetConditionPercent(i_atkTeam, i_atkIndex);
	const int def = m_data[i_defTeam].m_BaseDefenseSkills[i_defIndex] * GetConditionPercent(i_defTeam, i_defIndex);

	if(i_atkResult <= -30)
	{
		UpdateEval(i_atkTeam, i_atkIndex, 1, false);

		if		(result < -60)	UpdateEval(i_defTeam, i_defIndex, 2, true);
		else if	(result < -40)	UpdateEval(i_defTeam, i_defIndex, 1 + RandSelect(50, def), true);
		else if	(result < -20)	UpdateEval(i_defTeam, i_defIndex, 1, true);
		else if	(result <=  0)	UpdateEval(i_defTeam, i_defIndex, RandSelect(50, def), true);
		else if	(result <  20)	UpdateEval(i_defTeam, i_defIndex, 1, false);
		else if	(result <  40)	UpdateEval(i_defTeam, i_defIndex, 2, false);
		else					UpdateEval(i_defTeam, i_defIndex, 3, false);
	}
	else if(i_atkResult < 30)
	{
		if(i_atkResult <= -10)		UpdateEval(i_atkTeam, i_atkIndex, RandSelect(atk, 50), false);
		else if (i_atkResult >= 10)	UpdateEval(i_atkTeam, i_atkIndex, RandSelect(50, atk), true);

		if		(result < -60)	UpdateEval(i_defTeam, i_defIndex, 3, true);
		else if	(result < -40)	UpdateEval(i_defTeam, i_defIndex, 2, true);
		else if	(result < -20)	UpdateEval(i_defTeam, i_defIndex, 1 + RandSelect(50, def), true);
		else if	(result <=  0)	UpdateEval(i_defTeam, i_defIndex, 1, true);
		else if	(result <  20)	UpdateEval(i_defTeam, i_defIndex, RandSelect(def, 50), false);
		else if	(result <  40)	UpdateEval(i_defTeam, i_defIndex, 1, false);
		else					UpdateEval(i_defTeam, i_defIndex, 2, false);
	}
	else
	{
		UpdateEval(i_atkTeam, i_atkIndex, 1, true);

		if		(result < -60)	UpdateEval(i_defTeam, i_defIndex, 4, true);
		else if	(result < -40)	UpdateEval(i_defTeam, i_defIndex, 3, true);
		else if	(result < -20)	UpdateEval(i_defTeam, i_defIndex, 2, true);
		else if	(result <=  0)	UpdateEval(i_defTeam, i_defIndex, 1 + RandSelect(50, def), true);
		else if	(result <  20)	{}
		else if	(result <  40)	UpdateEval(i_defTeam, i_defIndex, RandSelect(50, def), false);
		else					UpdateEval(i_defTeam, i_defIndex, 1 + RandSelect(50, def), false);
	}
}

void OldMatch::ShotEvents(const int i_minute, const int i_team, const int i_shootingPlayer, const int i_attackResult, const int i_saveResult, const int i_shotType)
{
	switch(i_shotType)
	{
	case ShotNormal:
		AppendEvent(i_minute, "shots", GetPlayerDecoratedName(i_team, i_shootingPlayer, m_tempString));
		break;

	case ShotFreekick:
		AppendEvent(i_minute, "frekick", GetPlayerDecoratedName(i_team, i_shootingPlayer, m_tempString));
		break;

	case ShotPenaltykick:
		AppendEvent(i_minute, "penaltykick", GetPlayerDecoratedName(i_team, i_shootingPlayer, m_tempString));
		break;
	};

	const STL::string& name = m_data[1 - i_team].m_team->GetPlayersList().at(0)->GetName();

	if(i_attackResult <= G_SHOT_SUPER)
	{
		AppendEvent(i_minute, "shotsPlus2");
		if(i_saveResult <= G_SAVE_SUPER_SUPER) AppendEvent(i_minute, "saveSuperSuper", name);
		else if(i_saveResult <= G_SAVE_SUPER_GOOD)AppendEvent(i_minute, "saveSuperGood", name);
		else if(i_saveResult <= G_SAVE)AppendEvent(i_minute, "saveSuper", name);
		else if(i_saveResult <= G_SAVE_SUPER_MISSED){AppendEvent(i_minute, "goalSuper", CurrentResult(m_tempString)); AppendEvent(i_minute, "saveSuperMissed", name);}
		else if(i_saveResult <= G_SAVE_SUPER_RIDIC)AppendEvent(i_minute, "goalSuper", CurrentResult(m_tempString));
		else {AppendEvent(i_minute, "goalSuper", CurrentResult(m_tempString)); AppendEvent(i_minute, "saveSuperRidiculus", name);}
	}
	else if(i_attackResult <= G_SHOT_HARD)
	{
		AppendEvent(i_minute, "shotsPlus1");
		if(i_saveResult <= G_SAVE_HARD_SUPER) AppendEvent(i_minute, "saveHardSuper", name);
		else if(i_saveResult <= G_SAVE_HARD_GOOD) AppendEvent(i_minute, "saveHardGood", name);
		else if(i_saveResult <= G_SAVE) AppendEvent(i_minute, "saveHard", name);
		else if(i_saveResult <= G_SAVE_HARD_MISSED){AppendEvent(i_minute, "goalHard", CurrentResult(m_tempString)); AppendEvent(i_minute, "saveHardMissed", name);}
		else if(i_saveResult <= G_SAVE_HARD_RIDIC) AppendEvent(i_minute, "goalHard", CurrentResult(m_tempString));
		else {AppendEvent(i_minute, "goalHard", CurrentResult(m_tempString)); AppendEvent(i_minute, "saveHardRidiculus", name);}
	}
	else if(i_attackResult <= G_SHOT_NORM)
	{
		if(i_saveResult <= G_SAVE_NORM_SUPER) AppendEvent(i_minute, "saveNormalSuper", name);
		else if(i_saveResult <= G_SAVE_NORM_GOOD) AppendEvent(i_minute, "saveNormalGood", name);
		else if(i_saveResult <= G_SAVE_NORM_FEW) AppendEvent(i_minute, "saveNormal", name);
		else if(i_saveResult <= G_SAVE) AppendEvent(i_minute, "saveNormalFew", name);
		else if(i_saveResult <= G_SAVE_NORM_MISSED){AppendEvent(i_minute, "goalNormal", CurrentResult(m_tempString)); AppendEvent(i_minute, "saveNormalMissed", name);}
		else if(i_saveResult <= G_SAVE_NORM_RIDIC) AppendEvent(i_minute, "goalNormal", CurrentResult(m_tempString));
		else {AppendEvent(i_minute, "goalNormal", CurrentResult(m_tempString)); AppendEvent(i_minute, "saveNormalRidiculus", name);}
	}
	else if(i_attackResult < G_SHOT_EASY)
	{
		AppendEvent(i_minute, "shotsMinus1");
		if(i_saveResult <= G_SAVE_EASY_SUPER) AppendEvent(i_minute, "saveEasySuper", name);
		else if(i_saveResult <= G_SAVE_EASY_FEW) AppendEvent(i_minute, "saveEasy", name);
		else if(i_saveResult <= G_SAVE) AppendEvent(i_minute, "saveEasyFew", name);
		else if(i_saveResult <= G_SAVE_EASY_MISSED){AppendEvent(i_minute, "goalRidiculus", CurrentResult(m_tempString)); AppendEvent(i_minute, "saveEasyMissed", name);}
		else if(i_saveResult <= G_SAVE_EASY_RIDIC){AppendEvent(i_minute, "goalRidiculus", CurrentResult(m_tempString)); AppendEvent(i_minute, "saveEasyRidiculus", name);}
		else {AppendEvent(i_minute, "goalRidiculus", CurrentResult(m_tempString)); AppendEvent(i_minute, "saveEasyTurboRidiculus", name);}
	}
	else if(i_attackResult < G_SAVE_POST)
	{
		AppendEvent(i_minute, "posts");
	}
	else if(i_attackResult < G_SAVE_POST)
	{
		AppendEvent(i_minute, "shotsOut");
	}
	else
	{
		AppendEvent(i_minute, "shotsOutHeavy");
	}
}

void OldMatch::ActionEvents(const int i_minute, const int i_atkTeam, const int i_defTeam, 
			const int i_atkIndex, const int i_defIndex, const int i_atkResult, const int i_defResult)
{
	const int result = i_atkResult - i_defResult;
	AppendEvent(i_minute, "action", GetPlayerDecoratedName(i_atkTeam, i_atkIndex, m_tempString),
		GetPlayerDecoratedName(i_defTeam, i_defIndex, m_stringTemp));

	if(i_atkResult <= -30)
	{
		//if(i_atkResult <= -50)
		//	AppendEvent(i_minute, "attackRidicolous");
		//else 
		//	AppendEvent(i_minute, "attackWeak");

		if		(result < -60)	AppendEvent(i_minute, "defenseWeakSuper");
		else if	(result < -40)	AppendEvent(i_minute, "defenseWeakGood");
		else if	(result < -20)	AppendEvent(i_minute, "defenseWeakPlus");
		else if	(result <=  0)	AppendEvent(i_minute, "defenseWeakNormal");
		else if	(result <  20)	AppendEvent(i_minute, "defenseWeakMinus");
		else if	(result <  40)	AppendEvent(i_minute, "defenseWeakWeak");
		else					AppendEvent(i_minute, "defenseWeakRidicolous");
	}
	else if(i_atkResult < 30)
	{
		//if(i_atkResult <= -10)
		//	AppendEvent(i_minute, "attackMinus");
		//else if (i_atkResult < 10)
		//	AppendEvent(i_minute, "attackNormal");
		//else 
		//	AppendEvent(i_minute, "attackPlus");

		if		(result < -60)	AppendEvent(i_minute, "defenseNormalSuper");
		else if	(result < -40)	AppendEvent(i_minute, "defenseNormalGood");
		else if	(result < -20)	AppendEvent(i_minute, "defenseNormalPlus");
		else if	(result <=  0)	AppendEvent(i_minute, "defenseNormalNormal");
		else if	(result <  20)	AppendEvent(i_minute, "defenseNormalMinus");
		else if	(result <  40)	AppendEvent(i_minute, "defenseNormalWeak");
		else					AppendEvent(i_minute, "defenseNormalRidicolous");
	}
	else
	{
		//if(i_atkResult < 50)
		//	AppendEvent(i_minute, "attackGood");
		//else 
		//	AppendEvent(i_minute, "attackSuper");

		if		(result < -60)	AppendEvent(i_minute, "defenseGoodSuper");
		else if	(result < -40)	AppendEvent(i_minute, "defenseGoodGood");
		else if	(result < -20)	AppendEvent(i_minute, "defenseGoodPlus");
		else if	(result <=  0)	AppendEvent(i_minute, "defenseGoodNormal");
		else if	(result <  20)	AppendEvent(i_minute, "defenseGoodMinus");
		else if	(result <  40)	AppendEvent(i_minute, "defenseGoodWeak");
		else					AppendEvent(i_minute, "defenseGoodRidicolous");
	}
}

int OldMatch::SelectDefendingPlayer(const int i_team) const
{
	const int sum = m_data[i_team].m_RoleDefenseSelectorSum;
	if(sum == 0)
		return 0;

	int min = 0;
	const IntVec& def = m_data[i_team].m_RoleDefenseFactor;
	const int diceRoll = Rand()%sum;
	for(size_t i=1; i<def.size(); ++i)
	{
		min += def[i];
		if (diceRoll < min)
			return i;
	}
	
	return 0;
}

int OldMatch::SelectShootingPlayer(const int i_team) const
{
	return RandSelect(m_data[i_team].m_RoleAttackFactor, m_data[i_team].m_RoleAttackFactorSum);
}

void OldMatch::NotShot()
{
	for (int j=0; j<2; ++j)
	{
		const int goodDef = RandSelect(m_data[j].m_RoleDefenseFactor, m_data[j].m_RoleDefenseFactorSum);
		//const int noobDef = RandSelect(m_data[j].m_InvRoleAttackFactor, m_data[j].m_InvRoleAttackFactorSum);
		const int noobAtt = RandSelect(m_data[j].m_InvRoleAttackFactor, m_data[j].m_InvRoleAttackFactorSum);
		//const int goodAtt = RandSelect(m_data[j].m_RoleAttackFactor, m_data[j].m_RoleAttackFactorSum);
		UpdateEval(j, goodDef, RandSelect(50, m_data[j].m_RoleDefenseFactor[goodDef]), true);
		//UpdateEval(j, noobDef, RandSelect(m_data[j].m_RoleDefenseFactor[noobDef], 50), false);
		UpdateEval(j, noobAtt, RandSelect(m_data[j].m_RoleAttackFactor[noobAtt], 50), false);
		//UpdateEval(j, goodAtt, RandSelect(100, m_data[j].m_RoleAttackFactor[noobAtt]), true);
	}
}

bool OldMatch::DirectShot(const int i_minute, const size_t i_team, const bool i_produceEvents)
{
	const int goodAtt = RandSelect(m_data[i_team].m_RoleAttackFactor, m_data[i_team].m_RoleAttackFactorSum);
	const int noobDef = RandSelect(m_data[1-i_team].m_InvRoleAttackFactor, m_data[1-i_team].m_InvRoleAttackFactorSum);
	UpdateEval(i_team, goodAtt, RandSelect(50, m_data[i_team].m_RoleAttackFactor[goodAtt]), true);
	UpdateEval(1-i_team, noobDef, RandSelect(m_data[1-i_team].m_RoleDefenseFactor[noobDef], 50), false);

	const int shootingPlayerIndex = SelectShootingPlayer(i_team);

	return ShotPlayer(i_minute, i_team, shootingPlayerIndex, ShotNormal, i_produceEvents);
}

bool OldMatch::FreeKick(const int i_minute, const size_t i_team, const bool i_produceEvents)
{
	static int s_num_freekick=0;
	static int s_num_freekick_goal=0;

	s_num_freekick++;

	int shootingPlayerIndex = 0;
	int maxAtk = m_data[i_team].m_AttackSkills.at(0);

	for(size_t i=1; i<m_data[i_team].m_AttackSkills.size(); ++i)
	{
		const int atk = m_data[i_team].m_AttackSkills.at(i);
		if(atk > maxAtk)
		{
			shootingPlayerIndex = i;
			maxAtk = atk;
		}
	}

	const bool goal = ShotPlayer(i_minute, i_team, shootingPlayerIndex, ShotFreekick, i_produceEvents);

	if(goal)
		++s_num_freekick_goal;

	return goal;
}

bool OldMatch::PenaltyKick(const int i_minute, const size_t i_team, const bool i_produceEvents)
{
	static int s_num_penalty=0;
	static int s_num_penalty_goal=0;

	s_num_penalty++;

	int shootingPlayerIndex = 0;
	int maxAtk = m_data[i_team].m_BaseAttackSkills.at(0);

	for(size_t i=1; i<m_data[i_team].m_BaseAttackSkills.size(); ++i)
	{
		const int atk = m_data[i_team].m_BaseAttackSkills.at(i);
		if(atk > maxAtk)
		{
			shootingPlayerIndex = i;
			maxAtk = atk;
		}
	}

	const bool goal = ShotPlayer(i_minute, i_team, shootingPlayerIndex, ShotPenaltykick, i_produceEvents);

	if(goal)
		++s_num_penalty_goal;

	return goal;
}

bool OldMatch::ShotPlayer(const int i_minute, const size_t i_team, const int i_player, const int i_shotType, const bool i_produceEvents)
{
	bool goal = false;

	// Shot result: ok, out, post

	++m_data[i_team].m_shoots[i_player];
	++m_data[i_team].m_team_shoots;

	const int atkSkill = ((i_shotType==ShotPenaltykick) ? (3*m_data[i_team].m_BaseAttackSkills[i_player]) : m_data[i_team].m_AttackSkills[i_player]);
	const int shootPercent = STL::max(atkSkill, 10);
	const int upperDiceRoll = STL::max(100, atkSkill+10);
	const int diceroll = Rand() % upperDiceRoll;
	const int attackResult = diceroll - shootPercent;
	const bool shotInGoal = (attackResult < 0);
	
	int saveResult = 0;
	if (shotInGoal)
	{
		const int gkBasicSave = m_data[1 - i_team].m_gkSkill + 3*attackResult/4; //the better the shot is, the most hard it is to save
		const int savePercent = STL::max(STL::min(gkBasicSave, 99 + attackResult/2), 10);
		const int saveRoll = RandIntBetween(1, 100);
		saveResult = saveRoll - savePercent;
		if (saveResult > 0)
		{
			// goal!
			MatchTeamData& teamData = m_matchData.m_teamData[i_team];
			MatchPlayerData& playerData = teamData.m_playerData[i_player];
			goal = true;
			++teamData.m_goals;
			++playerData.m_goals;
			for(int i=0; i<m_numberOfTitolars; ++i)
			{
				const int* const wskill = m_data[i_team].m_team->GetPlayersList().at(i)->GetSkills().m_playerSkills.m_skills;
				UpdateEval(i_team, i, 1 + RandSelect(50, wskill[Defender]) + RandSelect(50, wskill[Attacker]), true);
				const int* const lskill = m_data[1-i_team].m_team->GetPlayersList().at(i)->GetSkills().m_playerSkills.m_skills;
				UpdateEval(1-i_team, i, RandSelect(lskill[Defender], 50) + RandSelect(lskill[Attacker], 50), false);
			}
			UpdateEval(i_team, i_player, 1 + RandSelect(50, m_data[i_team].m_RoleAttackFactor[i_player]), true); // goalscorer deserves special points more ;)
		}
	}

	// Evaluations
	CalcShotEval(i_team, i_player, attackResult, saveResult);
	
	// Events
	if(i_produceEvents)
		ShotEvents(i_minute, i_team, i_player, attackResult, saveResult, i_shotType);

	// Stamina
	UpdateStamina(i_team, i_player, i_minute, i_produceEvents, -1); // shooting player always looses stamina
	if(shotInGoal)
		UpdateStamina(1-i_team, 0, i_minute, i_produceEvents, -1); // gk looses stamina if there's a shot in goal

	return goal;
}

int OldMatch::Action(const int i_minute, const size_t i_team, const bool i_produceEvents)
{
	const int dTeam = 1-i_team;

	int atkIndex = SelectShootingPlayer(i_team);
	const int atkSkill = m_data[i_team].m_AttackSkills[atkIndex];
	const int atkRoll = Rand()%100;
	const int atkResult = atkSkill - atkRoll;
	++m_data[i_team].m_atk_total[atkIndex];
	++m_data[i_team].m_team_atk_total;

	int defIndex = SelectDefendingPlayer(dTeam);
	const int defSkill = m_data[dTeam].m_BaseDefenseSkills[defIndex];
	const int defRoll = Rand()%100;
	const int defResult = defSkill - defRoll;
	++m_data[dTeam].m_def_total[defIndex];
	++m_data[dTeam].m_team_def_total;

	// Evaluations
	CalcActionEval(i_team, dTeam, atkIndex, defIndex, atkResult, defResult);
	
	// Events
	if(i_produceEvents)
		ActionEvents(i_minute, i_team, dTeam, atkIndex, defIndex, atkResult, defResult);

	// Stamina
	UpdateStamina(i_team, atkIndex, i_minute, i_produceEvents, -1); 
	UpdateStamina(dTeam, defIndex, i_minute, i_produceEvents, -1);

	if(atkResult > defResult)
	{
		++m_data[i_team].m_atk_won[atkIndex];
		++m_data[i_team].m_team_atk_won;
		return atkIndex;
	}

	++m_data[dTeam].m_def_won[defIndex];
	++m_data[dTeam].m_team_def_won;
	return -1;
}

int OldMatch::CheckActions(const int i_minute, const bool i_produceEvents)
{
	const int attackFactorH = s_homeAttackFactor + STL::max(s_shotThreshold, m_data[0].m_RoleAttackFactorSum - m_data[1].m_RoleDefenseFactorSum/2);
	const int attackFactorA = s_awayAttackFactor + STL::max(s_shotThreshold, m_data[1].m_RoleAttackFactorSum - m_data[0].m_RoleDefenseFactorSum/2);
	const int shotProbability = (attackFactorH + attackFactorA) / 2;
	const int actionProbability = attackFactorH + attackFactorA;
	const int eventProbability = shotProbability + actionProbability;

	const int diceRollShot = Rand()%1000;
	if (diceRollShot > eventProbability)
	{
		NotShot();
		return NONE;
	}

	if(diceRollShot <= shotProbability)
	{
		const bool goal = DirectShot(i_minute, RandSelect(attackFactorH, attackFactorA), i_produceEvents);
		return goal ? GOAL : SHOT;
	}

	const int team = RandSelect(attackFactorH, attackFactorA);
	const int player = Action(i_minute, team, i_produceEvents);
	if(player>=0)
	{
		const bool goal = ShotPlayer(i_minute, team, player, ShotNormal, i_produceEvents);
		return goal ? GOAL : SHOT;
	}

	return ACTION;
}


